package com.summer.edu.service.impl;

import com.summer.R;
import com.summer.edu.entity.Course;
import com.summer.edu.entity.CourseDescription;
import com.summer.edu.entity.vo.CourseInfoVo;
import com.summer.edu.entity.vo.CoursePublishVo;
import com.summer.edu.mapper.CourseMapper;
import com.summer.edu.service.ChapterService;
import com.summer.edu.service.CourseDescriptionService;
import com.summer.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.edu.service.VideoService;
import com.summer.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-13
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VideoService videoService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);
        if (insert==0){
            throw  new GuliException(20001,"添加课程失败");
        }
        String cid = course.getId();
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        Course course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);
        CourseDescription description = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo vo) {
        Course course = new Course();
        BeanUtils.copyProperties(vo,course);
        int i = baseMapper.updateById(course);
        if (i==0){
            throw  new GuliException(20001,"修改课程信息失败");
        }
        CourseDescription description = new CourseDescription();
        description.setId(vo.getId());
        description.setDescription(vo.getDescription());
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        videoService.removeVideoByCourseId(courseId);
        chapterService.removeChapterByCourseId(courseId);
        courseDescriptionService.removeById(courseId);
        int i = baseMapper.deleteById(courseId);
        if (i==0){
            throw  new GuliException(20001,"删除失败");
        }
    }
}
